<template>
  <a-modal
    title="客户(商圈)管理"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="客户名称">
          <a-input :disabled="true" placeholder="客户名称"
                   v-decorator="[ 'name', {rules: [] }]"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="可提现技术服务费">
          <a-input :disabled="true" placeholder="可提现技术服务费" addonAfter="元"
                   v-decorator="[ 'profitAmount', {rules: [] }]"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="结算金额"
        >
          <a-input-search type="number" @search="onChangeFee" placeholder="结算金额"
                          v-decorator="[ 'settleAmount', {rules: [{ required: true, message: '请输入。。。' }]}]">
            <a-button slot="enterButton" type="primary">全部结算</a-button>
          </a-input-search>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import { getUseableAmount, createCustomerFlow} from '@/api/system/company'

  export default {
    data () {
      return {
        id: null,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        isDisabled: false,
        treeData: [],
        roleList: [],

        form: this.$form.createForm(this)
      }
    },
    mounted () {
    },
    methods: {
      edit (record) {
        this.id = record.id
        this.visible = true
        const { form: { setFieldsValue } } = this
        this.$nextTick(() => {
          let mdl = {}
          mdl.profitAmount = record.profitAmount * 0.01
          mdl.name = record.name
          setFieldsValue(mdl)
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            values.id = this.id
            createCustomerFlow(values).then((res) => {
              this.confirmLoading = false
              if (res.code === 200) {
                this.handleCancel()
                this.$emit('ok')
              }
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.visible = false
        this.isDisabled = false
        this.form.resetFields()
        this.id = null
      },
      onChangeFee () {
        //const _this = this
        getUseableAmount(this.id).then(res => {
          if (res.code == 200) {
            this.$nextTick(() => {
              this.form.setFieldsValue(pick(res.result, ['profitAmount', 'settleAmount']))
            })
          }

        })
      },
      onSearch () {
        const _this = this
        this.$confirm({
          title: '警告',
          content: `刷新之后会导致以前的接口凭证无法使用`,
          okText: '确定',
          okType: 'danger',
          cancelText: '取消',
          onOk () {
            // 在这里调用删除接口
            refreshApi(_this.id).then(record => {
              _this.$nextTick(() => {
                this.form.setFieldsValue(pick(record, ['apiKey']))
              })
            })
          },
          onCancel () {
            console.log('Cancel')
          }
        })
      }
    }
  }
</script>
